#include <QDebug>
#include "microblogmanager.h"
#include "http/cvhttp.h"
#include "util/cvutil.h"
#include "cvsession.h"

MicroBlogManager::MicroBlogManager()
{
}

void MicroBlogManager::createMicroblog(const QString &desc, int spaceId)
{
    if (desc.isNull() || desc.isEmpty())
    {
        qDebug() << "[createMicroblog] Description is empty";
        return;
    }
    CVSession *session = CVSession::get();
    QString networkUrl = session->getCurrentNetwork()->getUrl();
    if (networkUrl.isEmpty())
    {
        qDebug() << "[createMicroblog] Can not get network";
        return;
    }
    QUrl requestUrl = CVUtil::getBaseUrl("microblog.create_microblog", networkUrl);
    QByteArray bytes;
    bytes.begin();
    bytes.append("desc=").append(desc.toUtf8());
    bytes.append("&");
    bytes.append("spaceId=").append(QString::number(spaceId));
    bytes.end();
    CVHttp *http = new CVHttp(requestUrl, bytes);
    connect(http, SIGNAL(finished(QScriptValue,QString)), this, SLOT(parseData(QScriptValue,QString)));
}

void MicroBlogManager::parseData(QScriptValue json, const QString &msg)
{
    if (!json.isNull() && !json.isError())
    {
        emit handleData(true);
    }
    else
    {
        emit handleData(false);
    }
}
